<template>
    <div>
        <Title :title="title"></Title>
        <div ref="bottom3" style="height: 150px; margin-top: 15px;"></div>
    </div>
</template>
<script>
import { Line } from '@antv/g2plot';
import { get } from '../../utils/request';
import { mapState } from 'vuex';
import moment from 'moment';
export default {
    data() {
        return {
            title: '实时风速监测',
            params: {
                page: 1,
                pageSize: 51
            },
            windData: [],
            line: null
        }
    },
    computed: {
        ...mapState(['ed'])
    },
    watch: {
        ed: {
            handler(n, o) {
                this.params.device_id = n.device_id;
                this.params.engineer_id = n.engineer_id;
                this.getwindData();
                this.line.changeData(this.windData);
            },
            deep: true
        }
    },
    methods: {
        loadLine() {
            this.line = new Line(this.$refs.bottom3, {
                data: this.windData,
                xField: 'insert_time',
                yField: 'wind_speed',
                xAxis: {
                    label: {
                        style: {
                            fill: 'white'
                        }
                    }
                },
                yAxis: {
                    min:0,
                    max:10,
                    label: {
                        style: {
                            fill: 'white'
                        }
                    },
                    formatter: (v) => `${v} m/s`
                },
                label: {
                    style: {
                        fill: 'white'
                    }
                },
                animation: {
                    appear: {
                        animation: 'wave-in',
                        duration: 5000,
                    },
                },
                smooth: true,
                point: {
                    size: 5,
                    shape: 'diamond',
                    style: {
                        fill: 'white',
                        stroke: '#5B8FF9',
                        lineWidth: 2,
                    },
                },
                tooltip: {
                    domStyles: {
                        "g2-tooltip": {
                            backgroundColor: '#147',
                            color: 'white',
                            fontSize: 20
                        }
                    }
                },
                state: {
                    active: {
                        style: {
                            shadowBlur: 4,
                            stroke: '#000',
                            fill: 'red',
                        },
                    },
                },
                interactions: [{ type: 'marker-active' }],
            });
            this.line.render();
        },
        async getwindData() {
            let res = await get('/dashboard/pageQueryTodayData', this.params);
            res.data.list.forEach(function(item, index, arr) {
                arr[0].wind_speed = 1;
                item.insert_time = moment(item.insert_time).format('HH:mm:ss');
            });
            let temp = [];
            res.data.list.forEach((item, index, arr) => {
                if (index % 10 == 0) {
                    temp.push(arr[index])
                }
            });
            this.windData = temp;
        }
    },
    async mounted() {
        await this.getwindData();
        this.loadLine();
    }
}
</script>
<style lang="less" scoped>

</style>